package com.huhu.myharmony.ble;


import com.huhu.myharmony.utils.DataUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Description: 串口协议-消息格式
 *
 * @Author: HUlq
 * Date: 2021/9/28 17:48
 */
public class ProtocolEntity {

    /**
     * 字段      长度  说明
     * 协议标识   2    0xA55A：固定值, 串口协议标识
     * 协议版本   1    1~255：ver1~ver255
     * 消息类型   1    标识消息的类型，定义见表 4-2
     * 数据长度   2    数据字段长度，单位：字节
     * 数据      N     数据单元组，定义见表 4-3
     * 协议校验   1    从协议标识首节至协议内容尾字节求累加和后再对 256 取余
     */

    String protocolId = "A55A"; // 串口协议标识
    int version;//协议版本
    int messageType;//消息类型
    int dataLen;//数据长度,单位：字节
    Map<Integer, DeviceData> deviceListMap = new HashMap<>();
    int protocolVerify;//协议校验

    public String getProtocolId() {
        return protocolId == null ? "" : protocolId;
    }

    public void setProtocolId(String protocolId) {
        this.protocolId = protocolId;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public int getMessageType() {
        return messageType;
    }

    public void setMessageType(int messageType) {
        this.messageType = messageType;
    }

    public int getDataLen() {
        return dataLen;
    }

    public void setDataLen(int dataLen) {
        this.dataLen = dataLen;
    }

    public int getProtocolVerify() {
        return protocolVerify;
    }

    public void setProtocolVerify(int protocolVerify) {
        this.protocolVerify = protocolVerify;
    }

    public Map<Integer, DeviceData> getDeviceListMap() {
        return deviceListMap;
    }

    public void setDeviceListMap(Map<Integer, DeviceData> deviceListMap) {
        this.deviceListMap = deviceListMap;
    }

    public static class DeviceData {
        int tagId;
        int len;
        int value;

        public int getTagId() {
            return tagId;
        }

        public void setTagId(int tagId) {
            this.tagId = tagId;
        }

        public int getLen() {
            return len;
        }

        public void setLen(int len) {
            this.len = len;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }
    }

    public static ProtocolEntity get() {
        return new ProtocolEntity();
    }

    /**
     * 根据通用串口协议解析
     */
    public boolean parseCharacteristic(String hexStr) {
        try {
            String[] hexs = hexStr.split(" ");
            version = DataUtils.decodeHEX(hexs[2]);
            messageType = DataUtils.decodeHEX(hexs[3]);
            dataLen = DataUtils.decodeHEX(hexs[4] + hexs[5]);
            for (int i = 0; i < dataLen / 8; i++) {
                int startIndex = 6 + (i * 8);
                DeviceData deviceData = new DeviceData();
                deviceData.tagId = DataUtils.decodeHEX(hexs[startIndex] + hexs[startIndex + 1]);
                deviceData.len = DataUtils.decodeHEX(hexs[startIndex + 2] + hexs[startIndex + 3]);
                deviceData.value = DataUtils.decodeHEX(hexs[startIndex + 4] + hexs[startIndex + 5] +
                        hexs[startIndex + 6] + hexs[startIndex + 7]);
                deviceListMap.put(deviceData.tagId, deviceData);
            }
            protocolVerify = DataUtils.decodeHEX(hexs[6 + dataLen]);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
